﻿using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnWebsite.Models;
using PagedList;

namespace DoAnWebsite.Controllers
{
    public class HomeController : Controller
    {
        private static DoAnWebsite_Entities db = new DoAnWebsite_Entities();
        private static int PageSize = 10;
        //
        // GET: /Home/

        public ActionResult Index()
        {
            var bv = db.News.Where(x => x.Home && x.Active).OrderBy(m=>m.GroupNewsId);
            return View(bv);
        }

        public ActionResult GroupNewsDetail(int id)
        {
            ViewBag.GNewsId = id;
            var gNews = db.GroupNews.SingleOrDefault(x => x.GroupNewsId == id);
            ViewBag.GroupName = gNews.GNName;
            return View();
        }

        public ActionResult GroupNewsDetailPartial(int id, int? page)
        {
            ViewBag.GroupId = id;
            if (page == 0)
            {
                page = 1;
            }
            int pageNumber = (page ?? 1);
            var bv = db.News.Where(x => x.GroupNewsId == id).OrderBy(x => x.Date).ToPagedList(pageNumber, 1);
            ViewBag.pageNumber = pageNumber;
            return PartialView("GroupNewsDetailPartial", bv);
        }


        public ActionResult NewsDetail(int id)
        {
            var bv = db.News.SingleOrDefault(x => x.NewsId == id && x.Active);
            if (bv != null)
            {
                if (bv.Views == null) bv.Views = 1;
                else
                {
                    bv.Views = bv.Views + 1;
                }
                db.SaveChanges();
            }
            return View(bv);
        }

        public ActionResult HospitalInfo(int id)
        {
            var hospital = db.Hospitals.SingleOrDefault(x => x.HospitalId == id && x.IsActive);
            return View(hospital);
        }

        [HttpPost]
        public ActionResult Search(string keyword)
        {
            ViewBag.Keyword = keyword;
            return View();
        }

        public ActionResult SearchPartial(string id, int? page)
        {
            if (page == 0)
            {
                page = 1;
            }
            int pageNumber = (page ?? 1);
            var bv =
                db.News.Where(
                    m => m.Content.Contains(id) || m.Title.Contains(id) || m.Keyword.Contains(id)).OrderBy(x => x.Date).ToPagedList(pageNumber, PageSize);
            ViewBag.pageNumber = pageNumber;
            return PartialView("SearchPartial",bv);
        }

        public ActionResult ListHospital()
        {
            var hospital = db.Hospitals.Where(x=>x.IsActive).OrderBy(m=>m.Name);
            return View(hospital);
        }

        public ActionResult Question(int id)
        {
            var answer = db.Answers.SingleOrDefault(x => x.AskId == id && x.Active);
            return View(answer);
        }

        public ActionResult ListQuestion()
        {
            return View();
        }

        public ActionResult ListQuestionPartial(int? page)
        {
            if (page == 0)
            {
                page = 1;
            }
            int pageNumber = (page ?? 1);
            var ask = db.Asks.Where(x => x.Active).OrderBy(x => x.CreateDate).ToPagedList(pageNumber, PageSize);
            ViewBag.pageNumber = pageNumber;
            return PartialView("ListQuestionPartial",ask);
        }

        public ActionResult CancerInfo(int id)
        {
            var cancer = db.Cancers.SingleOrDefault(x => x.CancerId == id);
            ViewBag.GroupName = cancer.CancerName;
            ViewBag.CancerId = id;
            return View();
        }

        public ActionResult CancerInfoPartial(int id, int? page)
        {
            var cancer = db.Cancers.SingleOrDefault(x => x.CancerId == id);
            ViewBag.GroupName = cancer.CancerName;
            if (page == 0)
            {
                page = 1;
            }
            int pageNumber = (page ?? 1);
            var bv = db.News.Where(x => x.CancerId == id).OrderBy(x => x.Date).ToPagedList(pageNumber, PageSize);
            ViewBag.pageNumber = pageNumber;
            return PartialView("CancerInfoPartial",bv);
        }

        public ActionResult ListDoc()
        {
            return View();
        }

        public ActionResult ListDocPartial(int? page)
        {
            if (page == 0)
            {
                page = 1;
            }
            int pageNumber = (page ?? 1);
            var doc = db.Documents.Where(x => x.Active).OrderBy(x => x.CreateDate).ToPagedList(pageNumber, PageSize);
            ViewBag.pageNumber = pageNumber;
            return PartialView("ListDocPartial",doc);
        }

        public ActionResult ListVideo()
        {
            return View();
        }

        public ActionResult ListVideoPartial(int? page)
        {
            if (page == 0)
            {
                page = 1;
            }
            int pageNumber = (page ?? 1);
            var video = db.Videos.Where(x => x.Active).OrderBy(x => x.Date).ToPagedList(pageNumber, PageSize);
            ViewBag.pageNumber = pageNumber;
            return PartialView("ListVideoPartial",video);
        }

        public ActionResult VideoDetail(int id)
        {
            var bv = db.Videos.SingleOrDefault(x => x.VideoId == id && x.Active);
            if (bv != null)
            {
                if (bv.Views == null) bv.Views = 1;
                else
                {
                    bv.Views = bv.Views + 1;
                }
                db.SaveChanges();
            }
            return View(bv);
        }

        public ActionResult DowbloadFile(int id)
        {
            var doc = db.Documents.SingleOrDefault(x => x.DocumentId == id && x.Active);
            try
            {
                if (doc.Download == null) doc.Download = 1;
                else
                {
                    doc.Download = doc.Download + 1;
                }
                db.SaveChanges();
                var fs = System.IO.File.OpenRead(Server.MapPath("../.." + doc.File));
                string extention = Path.GetExtension(doc.File);
                if (extention != null) extention = extention.Replace(".", "");
                string contentType = "application/"+extention;
                return File(fs, contentType, doc.Name);
            }
            catch
            {
                throw new HttpException(404, "Couldn't find " + doc.File);
            }
        }

        public ActionResult SearchbyTag(int id)
        {
            var tag = db.Tags.SingleOrDefault(x => x.TagId == id);
            string keyword = tag.TagString;
            ViewBag.Keyword = keyword;
            ViewBag.TagId = id;
            return View();
        }

        public ActionResult SearchbyTagPartial(string id, int? page)
        {
            if (page == 0)
            {
                page = 1;
            }
            int pageNumber = (page ?? 1);
            var bv =
                db.News.Where(
                    m => m.Content.Contains(id) || m.Title.Contains(id) || m.Keyword.Contains(id)).OrderBy(x => x.Date).ToPagedList(pageNumber, PageSize);
            ViewBag.pageNumber = pageNumber;
            return PartialView("SearchbyTagPartial",bv);
        }
    }
}
